// 获取dayCount前时间

// 时间加减天数
import {conUtils} from "./conUtils";
import {timeFormat} from "uview-plus";

export const getCountDate = (dayCount = 0) => {
    const dd = new Date()
    dd.setDate(dd.getDate() + dayCount) //获取dayCount天后的日期
    const year = dd.getFullYear()
    const month = dd.getMonth() + 1 //获取当前月份的日期
    const day = dd.getDate()
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`
}
export const getCountDate2 = (dayCount = 0) => {
    const dd = new Date()
    dd.setDate(dd.getDate() + dayCount) //获取dayCount天后的日期
    const year = dd.getFullYear()
    const month = dd.getMonth() + 1 //获取当前月份的日期
    const day = dd.getDate()
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`
}
// 时间催转换年-月-日 时-分-秒
export const timestampToDate = (timestamp) => {
    return timeFormat(timestamp, 'yyyy-mm-dd hh:MM:ss')
}
// 时间加减小时
export function getNewTimeV2(hourCount) {
    const time = new Date();
    return new Date(time.getFullYear(), time.getMonth(), time.getDate(), time.getHours() + hourCount, time.getMinutes(), time.getSeconds());
}

// 获取倒计时时间
export const handleGetTime = (data) => {
    isAmOrPm(data)
    let begin_end = 0 //判断活动开始和未开始 0未开始   1已开始  2已结束,
    let nowTime = +new Date(); // 当前时间戳
    let year = new Date().getFullYear()
    let month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
    let day = new Date().getDate() < 10 ? '0' + (new Date().getDate()) : new Date().getDate()
    let currentDate = year + '/' + month + '/' + day // 获取当前时间的y/m/d 拼接活动的00:00:00
    let dec;
    // 获取早上的开始时间戳和结束时间戳
    let amStartTime = +new Date(currentDate + ' ' + data.am_start_time)
    let amEndTime = +new Date(currentDate + ' ' + data.am_end_time)
    // 结束

    // 获取下午的开始时间戳和结束时间戳
    let pmStartTime = +new Date(currentDate + ' ' + data.pm_start_time)
    let pmEndTime = +new Date(currentDate + ' ' + data.pm_end_time)
    // 结束

    // 计算上午时间

    if (nowTime >= amStartTime && nowTime <= amEndTime && data.am_start_time !== data.am_end_time) {
        begin_end = 1
        let end_time = currentDate + ' ' + data.am_end_time
        let putTime = +new Date(end_time.replace(/-/g, "/"));
        dec = (putTime - nowTime)
        console.log('早上活动开始了')
    } else {
        if (nowTime < amStartTime && data.am_start_time !== data.am_end_time) {
            begin_end = 2
            let start_time = currentDate + ' ' + data.am_start_time
            let putTime = +new Date(start_time.replace(/-/g, "/"));
            dec = (putTime - nowTime)
            console.log('早上活动还没有开始')
        }
    }

    // 计算下午时间
    if (nowTime >= pmStartTime && nowTime <= pmEndTime && data.pm_end_time !== data.pm_start_time) {
        begin_end = 1
        let end_time = currentDate + ' ' + data.pm_end_time
        let putTime = +new Date(end_time.replace(/-/g, "/"));
        dec = (putTime - nowTime)
        console.log('下午活动开始了')
    } else {
        if (nowTime < pmStartTime && nowTime > amEndTime && data.pm_end_time !== data.pm_start_time) {
            begin_end = 2
            let start_time = currentDate + ' ' + data.pm_start_time
            let putTime = +new Date(start_time.replace(/-/g, "/"));
            dec = (putTime - nowTime)
            console.log('下午活动还没有开始')
        }
        if (nowTime > pmEndTime && data.pm_end_time !== data.pm_start_time) {
            begin_end = 3
            console.log('今日活动已结束')
        }
    }

    conUtils("------dec------", dec)
    conUtils("------begin_end------", begin_end)
    return [dec, begin_end]
}
export const compareTime = (time1, time2) => {
    // 将时间转换为小时和分钟
    const parts1 = time1.split(':').map(Number);
    const parts2 = time2.split(':').map(Number);

    // 比较小时
    if (parts1[0] > parts2[0]) {
        return time1;
    } else if (parts1[0] < parts2[0]) {
        return time2;
    }

    // 如果小时相等，比较分钟
    if (parts1[1] > parts2[1]) {
        return time1;
    } else if (parts1[1] < parts2[1]) {
        return time2;
    }

    // 如果小时和分钟都相等，比较秒
    if (parts1[2] > parts2[2]) {
        return time1;
    } else if (parts1[2] < parts2[2]) {
        return time2;
    }

    return time1; //'equal'
}

export const letTime = (time1, time2) => {
    // 将时间转换为小时和分钟
    const parts1 = time1.split(':').map(Number);
    const parts2 = time2.split(':').map(Number);

    // 比较小时
    if (parts1[0] > parts2[0]) {
        return time2;
    } else if (parts1[0] < parts2[0]) {
        return time1;
    }

    // 如果小时相等，比较分钟
    if (parts1[1] > parts2[1]) {
        return time2;
    } else if (parts1[1] < parts2[1]) {
        return time1;
    }

    // 如果小时和分钟都相等，比较秒
    if (parts1[2] > parts2[2]) {
        return time2;
    } else if (parts1[2] < parts2[2]) {
        return time1;
    }

    return time1; //'equal'
}

// 0 表示早上的活动 1表示下午的活动 2活动还没开始 3活动已结束 4未知
const isAmOrPm = (data) => {
    let type = 2
    let nowTime = +new Date(); // 当前时间戳
    const currentDate = getCurrentDate()
    // 获取早上的开始时间戳和结束时间戳
    let amStartTime = +new Date(currentDate + ' ' + data.am_start_time)
    let amEndTime = +new Date(currentDate + ' ' + data.am_end_time)
    // 获取下午的开始时间戳和结束时间戳
    let pmStartTime = +new Date(currentDate + ' ' + data.pm_start_time)
    let pmEndTime = +new Date(currentDate + ' ' + data.pm_end_time)
    conUtils("------nowTime------", nowTime)
    conUtils("------amStartTime------", amStartTime)
    conUtils("------amEndTime------", amEndTime)
    conUtils("------pmStartTime------", amStartTime)
    conUtils("------pmEndTime------", amEndTime)
    if (nowTime <= amEndTime && nowTime >= amStartTime) {
        // 早上的活动
        type = 0
    } else if (nowTime <= pmEndTime && nowTime >= pmStartTime) {
        // 下午的活动
        type = 1
    } else {
        if (nowTime > pmEndTime) {
            // 活动已结束
            type = 3
        } else if (nowTime < amStartTime) {
            // 活动还没开始
            type = 2
        } else {
            type = 4
        }
    }
    conUtils("活动状态：", type)
    return type
}

const getCurrentDate = () => {
    let year = new Date().getFullYear()
    let month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
    let day = new Date().getDate() < 10 ? '0' + (new Date().getDate()) : new Date().getDate()
    return year + '/' + month + '/' + day // 获取当前时间的y-m-d 拼接活动的00:00:00
}

// 获取今日日期
export const getDayDate = () => {
    const date = new Date();
    const seperator1 = "-";
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    return year + seperator1 + month
}


export function getDaysBetweenStop(dateStr) {
    const currentDateYear = new Date().getFullYear()
    const currentDateMonth = new Date().getMonth() + 1
    const currentDateDay = new Date().getDate()
    const currentDate = new Date(
        currentDateYear + '/' + currentDateMonth + '/' + currentDateDay,
    ).getTime() // 获取时间戳
    const date1Time = new Date(dateStr).getTime() // 获取时间戳
    const ONE_DAY = 1000 * 60 * 60 * 24 // 一天的毫秒数
    const difference = date1Time - currentDate // 获取时间差
    return Math.round(difference / ONE_DAY) // 两个日期之间的天数
}

// 获取今日时分秒（转毫秒）
export const getTime = (startTime, endTime) => {
    const startTimeStr = startTime.split(':')
    const endTimeStr = endTime.split(':')

    const startHours = startTimeStr[0]
    const startMinutes = startTimeStr[1]
    const startSeconds = startTimeStr[2]

    const currenStartTime = parseInt(startHours) * 60 * 60 * 1000 + parseInt(startMinutes) * 60 * 1000 +  parseInt(startSeconds) * 1000
    console.log("====================================================================")
    console.log("startHours",startHours)
    console.log("startMinutes",startMinutes)
    console.log("startSeconds",startSeconds)
    console.log("currenStartTime",currenStartTime)

    const endHours = endTimeStr[0]
    const endMinutes = endTimeStr[1]
    const endSeconds = endTimeStr[2]
    const currenEndTime = parseInt(endHours) * 60 * 60 * 1000 + parseInt(endMinutes) * 60 * 1000 + parseInt(endSeconds) * 1000
    console.log("endHours",endHours)
    console.log("endMinutes",endMinutes)
    console.log("endSeconds",endSeconds)
    console.log("currenEndTime",currenEndTime)

    const hours = new Date().getHours()
    const minutes = new Date().getMinutes()
    const seconds = new Date().getSeconds()
    const currenTime = hours * 60 * 60 * 1000 + minutes * 60 * 1000 + seconds * 1000
    console.log("hours",hours)
    console.log("minutes",minutes)
    console.log("seconds",seconds)
    console.log("currenTime",currenTime)

    console.log("currenTime > currenStartTim",currenTime > currenStartTime)
    console.log("currenTime < currenEndTime",currenTime < currenEndTime)
    console.log("====================================================================")
    return  currenTime > currenStartTime && currenTime < currenEndTime
}

/**
 * @description 时间戳转为多久之前
 * @param {String|Number} timestamp 时间戳
 * @param {String|Boolean} format
 * 格式化规则如果为时间格式字符串，超出一定时间范围，返回固定的时间格式；
 * 如果为布尔值false，无论什么时间，都返回多久以前的格式
 * @returns {string} 转化后的内容
 */
export function timeFrom(timestamp = null, format = 'yyyy-mm-dd') {
    if (timestamp == null) timestamp = Number(new Date())
    timestamp = parseInt(timestamp)
    // 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
    if (timestamp.toString().length === 10) timestamp *= 1000
    let timer = (new Date()).getTime() - timestamp
    timer = parseInt(timer / 1000)
    // 如果小于5分钟,则返回"刚刚",其他以此类推
    let tips = ''
    switch (true) {
        case timer < 300:
            tips = '刚刚'
            break
        case timer >= 300 && timer < 3600:
            tips = `${parseInt(timer / 60)}分钟前`
            break
        case timer >= 3600 && timer < 86400:
            tips = `${parseInt(timer / 3600)}小时前`
            break
        case timer >= 86400 && timer < 2592000:
            tips = `${parseInt(timer / 86400)}天前`
            break
        default:
            // 如果format为false，则无论什么时间戳，都显示xx之前
            if (format === false) {
                if (timer >= 2592000 && timer < 365 * 86400) {
                    tips = `${parseInt(timer / (86400 * 30))}个月前`
                } else {
                    tips = `${parseInt(timer / (86400 * 365))}年前`
                }
            } else {
                tips = timeFormat(timestamp, format)
            }
    }
    return tips
}

// 计算两个日期相差天数
export function calculateDateDifferenceFun(date1, date2){
    // console.log('起始日期：', date1, '结束日期：', date2)
    // 将输入的日期字符串转换为Date对象
    const startDate = new Date(date1);
    const endDate = new Date(date2);

    // 获取时间戳（毫秒数）
    const diffTime = Math.abs(endDate.getTime() - startDate.getTime());

    // 计算天数差
    return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
//倒计时计算
export function countDownFun(item){
    console.log('最终时间=',item)
    let nowTime = +new Date()
    let putTime = +new Date(item.end_time.replace(/-/g, "/"))

    return (putTime - nowTime)
}